<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Backend BanController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class Backend_BanController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
		$this->view->headLink()->appendStylesheet($this->baseUrl . '/static/css/default/jquery.css');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-ui-1.7.2.custom.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/date.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery.datePicker.js');
	}
	
	
	
	/*
	 * List bans
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		// helper
		$baseurl	= new MHDEV_View_Helper_BaseUrl();
		
		// vars
		$count		= 10; 
		$page		= intval($this->getRequest()->getParam('paget', 0));
		
		// db
		$bans		= new Bans();
		
		// query
		$getBans	= $bans->fetchAll(null, 'create_date DESC');
			
		// paginator
        $pageBans	= Zend_Paginator::factory($getBans->toArray());
        $pageBans->setItemCountPerPage($count);
        $pageBans->setCurrentPageNumber($page);
        
		// view
		$this->view->results	= $pageBans;
		$this->view->count		= $getBans->count();
	}
	
	
	
	/*
	 * Edit a ban
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function editAction()
	{
		// param
		$uid		= $this->_request->getParam('uid', 0);
		
		// vars
		$postData 	= $this->_request->getPost();
		
		// helper
		$postText	= new MHDEV_View_Helper_GetPostText();
		
		// db
		$bans	= new Bans();
		$boards	= new Boards();
		
		// query
		$getBan	= $bans->find($uid);
		$resBan	= $getBan->toArray();
		
		// form
		$form	= new BanForm();
		
		// get post		
		if ($this->_request->isPost() && $form->isValid($postData))
		{
			$form->removeElement('submit');
			
			$bans->update($form->getValues(), 'uid=' . $uid);
			
			// log action
			Zend_Registry::get('log')->info('[MODERATE] Ban with ID ' . $uid . ' was changed by Admin-ID ' . $this->auth->getIdentity()->uid);
			$this->_redirect('/moderation.html#tabs-4');
		}
		else
		{
			// assign boards to form
			$getBoard 	= $form->getElement('board_uid');
			if($boards->fetchAll()->count() > 0)
			{
				foreach($boards->fetchAll()->toArray() AS $result)
				{
					$getBoard->addMultiOption($result['uid'], $result['title']);
				}
			}
			
			$form->getElement('user_uid')->setValue($resBan[0]['user_uid']);
			$form->getElement('thread_uid')->setValue($resBan[0]['thread_uid']);
			
			$form->populate($resBan[0]);
		}
		
		$this->view->post	= $postText->GetPostText($resBan[0]['post_uid']);
		$this->view->count	= $getBan->count();
		$this->view->form 	= $form;
	}
}
?>